
#include "board.h"
#include "ev_utils.h"
#include "ev_tick_hook.h"
#include "ev_console.h"
#include "ev_trans.h"
#include "ev_comm.h"


static void bootloader_process(void)
{
    ev_packet_t packet;
    while(1) {
        if (!ev_trans_rx(&packet, 200)) {
            ev_comm_process(&packet);
        }
        driv_run_led_op(-1);
    }
}

int main()
{
    /* init board */
    driv_board_init(0);
    hw_delay_init();
    driv_run_led_init();
    ev_console_init(115200);
    ev_trans_init(115200);

    /* print board info */
    ev_printf("Bootloader start...\n");
    ev_printf("Build Time: %s, %s\n", __DATE__, __TIME__);

    /* check there APP1_BASE is empty */
    if (*(uint32_t *)APP1_BASE == 0xFFFFFFFF) {
        bootloader_process();
    }

    /* check if there is a command */
    if (ev_comm_get_boot_type() == 0xca) {
        bootloader_process();
    }

    ev_printf("Run application ...\n");
    hw_delay_ms(5); //wait for console flush
    driv_board_deinit(0);
    driv_jump_to_app(APP1_BASE);

    return 0;
}

void SysTick_Handler(void)
{
    uint32_t flag = driv_disable_irq();

    ev_tick_hook_schedule();
    driv_cpu_reboot_check();
    driv_tick_inc();

    driv_enable_irq(flag);
}
